public class Solution541 {
    public String reverse(String s){
        String ans="";
        for (int i=s.length()-1;i>=0;i--){
            ans=ans+s.charAt(i);
        }
        return ans;
    }

    public String reverseStr(String s, int k) {
        String s1="" ;
        for (int i=0;i<s.length();i=i+2*k){
            String s2 = s.substring(i, i + k);
            if (i+2*k<=s.length()){
                s1=s1+reverse(s2)+s.substring(i+k,i+2*k);
                continue;
            }
            if (i+k<=s.length()&&i+2*k>s.length()){
                s1=s1+reverse(s2)+s.substring(i+k);
                continue;
            }
            s1=s1+reverse(s.substring(i));
        }
        return s1;
    }

    public static void main(String[] args) {
        System.out.println(new Solution541().reverseStr("abcdefg",2));
    }
}
